import React from 'react'
// import styles from './SnippetTab.styl'
// import CSSModules from '../../../lib/CSSModules'
import dataApi from '../../../main/lib/dataApi'
import i18n from '../../../lib/i18n'
import eventEmitter from '../../../main/lib/eventEmitter'
import context from '../../../lib/context'

class SnippetList extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      snippets: []
    }
  }

  componentDidMount() {
    this.reloadSnippetList()
    eventEmitter.on('snippetList:reload', this.reloadSnippetList.bind(this))
  }

  reloadSnippetList() {
    dataApi.fetchSnippet().then(snippets => {
      this.setState({ snippets })
      this.props.onSnippetSelect(this.props.currentSnippet)
    })
  }

  handleSnippetContextMenu(snippet) {
    context.popup([
      {
        label: i18n.__('Delete snippet'),
        click: () => this.deleteSnippet(snippet)
      }
    ])
  }

  deleteSnippet(snippet) {
    dataApi
      .deleteSnippet(snippet)
      .then(() => {
        this.reloadSnippetList()
        this.props.onSnippetDeleted(snippet)
      })
      .catch(err => {
        throw err
      })
  }

  handleSnippetClick(snippet) {
    this.props.onSnippetSelect(snippet)
  }

  createSnippet() {
    dataApi
      .createSnippet()
      .then(() => {
        this.reloadSnippetList()
        // scroll to end of list when added new snippet
        const snippetList = document.getElementById('snippets')
        snippetList.scrollTop = snippetList.scrollHeight
      })
      .catch(err => {
        throw err
      })
  }

  defineSnippetStyleName(snippet) {
    const { currentSnippet } = this.props

    if (currentSnippet == null) {
      return 'snippet-item'
    }

    if (currentSnippet.id === snippet.id) {
      return 'snippet-item-selected'
    } else {
      return 'snippet-item'
    }
  }

  render() {
    const { snippets } = this.state
    return (
      <div className='snippet-list'>
        <div className='group-section'>
          <div className='group-section-control'>
            <button
              className='group-control-button'
              onClick={() => this.createSnippet()}
            >
              <i className='fa fa-plus' /> {i18n.__('New Snippet')}
            </button>
          </div>
        </div>
        <ul id='snippets' className='snippets'>
          {snippets.map(snippet => (
            <li
              className={this.defineSnippetStyleName(snippet)}
              key={snippet.id}
              onContextMenu={() => this.handleSnippetContextMenu(snippet)}
              onClick={() => this.handleSnippetClick(snippet)}
            >
              {snippet.name}
            </li>
          ))}
        </ul>
      </div>
    )
  }
}

export default SnippetList;// CSSModules(SnippetList, styles)
